package com.zhouke.chatgpt;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.squareup.moshi.Moshi;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageRole;
import com.theokanning.openai.service.OpenAiService;
import lombok.Data;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Component
public class Responder {
    @Value("${openai.api.key}")
    private String apiKey;
    private OpenAiService service;
    public Responder(){}
    public String generateResponse(String prompt)throws IOException {
        Map<String,String> headers = new HashMap<String,String>();
        headers.put("Content-Type","application/json;charset=UTF-8");
        JSONObject json = new JSONObject();
        //选择模型
        json.set("model","text-davinci-003");
        //添加我们需要输入的内容
        json.set("prompt",prompt);
        json.set("temperature",1.0);
        json.set("max_tokens",100);
        json.set("top_p",1);
        json.set("n",1);
        json.set("stream",false);
        HttpResponse response = HttpRequest.post("https://api.openai.com/v1/completions")
                .setHttpProxy("127.0.0.1",7890)
                .headerMap(headers, false)
                .bearerAuth("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")
                .body(String.valueOf(json))
                .timeout(5 * 60 * 1000)
                .execute();
//        InputStream inputStream = response.bodyStream();
        return response.body();
    }
}
